package Q1;

import java.io.*;
import java.util.ArrayList;

public class StudentData {

    public ArrayList<Student> studentArr;

    public StudentData(ArrayList<Student> studentArr) {
        this.studentArr = studentArr;
    }

    public StudentData() {
    }


    public Student QueryID(int ID) {
        for (Student s1 : studentArr)
            if (s1.getID() == ID) {
                return s1;
            }
        return null;
    }

    public void addStudent(Student s) {
        if (QueryID(s.getID()) != null) {
            System.out.println("学号重复,添加失败！");
            return;
        }
        studentArr.add(s);
        System.out.println(s + "添加成功");
    }

    public void deleteStudent(Student s) {
        boolean flag = studentArr.remove(s);
        System.out.println(flag ? "删除成功" : "没有找到该学生");
    }

    //把src 学生 替换 dst 学生
    public void replaceStudent(Student src, Student dst) {
        if (QueryID(dst.getID()) != null) {
            studentArr.remove(dst);
            studentArr.add(src);
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败，没有这样的学生");
        }
    }

    //查找特定学生， 通过学号、姓名、年龄查找
    public ArrayList<Student> filterStudent(String by, String content) {
        ArrayList<Student> result = new ArrayList<Student>();
        for (Student s : studentArr) {
            switch (by) {
                case "ID": {
                    if (s.getID() == Integer.parseInt(content)) result.add(s);
                    break;
                }
                case "name": {
                    if (s.getName().equals(content)) result.add(s);
                    break;
                }
                case "age": {
                    if (s.getAge() == Integer.parseInt(content)) result.add(s);
                    break;
                }
                default: {
                    System.out.println("不支持的搜索类型");
                    return result;
                }
            }

        }
        return result;
    }
    //保存到save数据
    public void save() {
        ObjectOutputStream stream = null;
        try {
            stream = new ObjectOutputStream(new FileOutputStream("StudentData.save"));
            stream.writeObject(studentArr);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("保存数据库");
    }
    //读取数据
    public void load() {
        ObjectInputStream stream = null;
        try {
            stream = new ObjectInputStream(new FileInputStream("StudentData.save"));
            studentArr = (ArrayList<Student>) stream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("读取数据库。。。");

    }
}
